package com.xmad.ksadutils.AdSDK.AdMold.Kwai;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsAdVideoPlayConfig;
import com.kwad.sdk.api.KsFeedAd;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.xmad.ksadutils.AdSDK.AdBase.AdBase;
import com.xmad.ksadutils.AdSDK.Utlis.Tool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KsLoadFeedAd_2 extends AdBase {
    private volatile static KsLoadFeedAd_2 mInstance;
    KsFeedAd Ad;
    static Map<Long, AdComplete> mMap= new HashMap<Long,AdComplete>();
    /**
     * 单例模式
     *
     * @return
     */
    public static KsLoadFeedAd_2 getInstance() {
        if (mInstance == null) {
            synchronized (KsLoadFeedAd_2.class) {
                if (mInstance == null) {
                    mInstance = new KsLoadFeedAd_2();
                }
            }
        }
        return mInstance;
    }


    @Override
    protected void load() {
        if (getKSid()==null||getKSid().length==0){
            Log.e(TAG,"先设置梯度ID");
            return;
        }
        //新建
        NewMap();
        //获取广告对象List列表
        GetAdList();
    }

    //新建
    public void NewMap(){
        Log.e(TAG,mMap.toString());
        if (mMap.size()==0){
            for (Long Id:getKSid()){
                mMap.put(Id,null);
            }
        }
    }

    //获取广告对象List列表
    public void GetAdList(){
        for(Long key:mMap.keySet()){
            if (mMap.get(key)==null||mMap.get(key).getAd()==null){
                getAds(key);
            }
        }
    }

    private void   getAds(Long id){
        Log.e(TAG,"KsFeedAd加载"+ id);
        KsScene scene = new KsScene.Builder(id).adNum(1).build();
        KsAdSDK.getLoadManager()
                .loadFeedAd(scene, new KsLoadManager.FeedAdListener() {
                    @Override
                    public void onError(int code, String msg) {
                        Log.e(TAG,"KsFeedAd广告请求失败"+ code + msg);
                        if (listener!=null){
                            listener.onError(code+msg);
                        }
                    }

                    @Override
                    public void onFeedAdLoad(@Nullable List<KsFeedAd> adList) {
                        if (adList == null || adList.isEmpty()) {
                            Log.e(TAG, "广告数据为空");
                            return;
                        }
                        for (KsFeedAd ksFeedAd : adList) {
                            if (ksFeedAd == null) { continue; }
                            KsAdVideoPlayConfig videoPlayConfig = new KsAdVideoPlayConfig.Builder().videoSoundEnable(true) // 是否有声播放
                                    .dataFlowAutoStart(false) // 是否非WiFi下自动播放
                                    .build();
                            ksFeedAd.setVideoPlayConfig(videoPlayConfig);
                            Ad=adList.get(0);
                            mMap.put(id,new AdComplete(true,Ad));
                            if (!isReveal()){
                                return;
                            }
                            View view = null;
                            for(Long key:mMap.keySet()) {
                                if (mMap.get(key) != null && mMap.get(key).getAd() != null) {
                                    view=mMap.get(key).ad.getFeedView(activity);
                                    break;
                                }
                            }
                            frameLayout.removeAllViews();
                            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                            lp.gravity = Gravity.CENTER;
                            frameLayout.addView(view,lp);
                           // frameLayout.addView(Ads.getFeedView(activity),lp);
                            frameLayout.setPadding(10,10,10,10);
                            ksFeedAd.setAdInteractionListener(new KsFeedAd.AdInteractionListener() {
                                @Override
                                public void onAdClicked() {
                                    Log.e(TAG, "广告点击回调");
                                }

                                @Override
                                public void onAdShow() {
                                    if (listener!=null){
                                        listener.onAdShow();
                                    }
                                    Ad=null;
                                }

                                @Override
                                public void onDislikeClicked() {
                                    Log.e(TAG, "广告不喜欢回调");
                                    adList.remove(ksFeedAd);
                                }

//                                @Override
//                                public void onDownloadTipsDialogShow(){
//
//                                }
//
//                                @Override
//                                public void onDownloadTipsDialogDismiss(){
//
//                                }
                            });
                        }
                    }
                });
    }

    @Override
    protected void start() {

    }

    public class AdComplete{
        private boolean Complete;
        private KsFeedAd ad;

        public AdComplete(boolean complete, KsFeedAd ad) {
            Complete = complete;
            this.ad = ad;
        }

        public boolean isComplete() {
            return Complete;
        }

        public void setComplete(boolean complete) {
            Complete = complete;
        }

        public KsFeedAd getAd() {
            return ad;
        }

        public void setAd(KsFeedAd ad) {
            this.ad = ad;
        }
    }

}
